//
//  MessageViewController.m
//  QQ界面
//
//  Created by apple on 16/10/22.
//  Copyright © 2016年 LWB. All rights reserved.
//

#import "MessageViewController.h"
#import "PhoneViewController.h"
#import "LWBNavgaViewController.h"
#import "MessageTableViewCell.h"
#import "InterfaceMessage.h"
#import "ChatViewController.h"
@interface MessageViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UIImageView *addImag;
@property (nonatomic,strong)NSMutableArray*friendsMess;
@property (nonatomic,strong)UITableView *friendstab;
@end

@implementation MessageViewController
-(NSMutableArray *)messages{
    if (_friendsMess ==nil) {
        NSString *path = [[NSBundle mainBundle]pathForResource:@"interfacemessages.plist" ofType:nil];
        NSArray *array = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *interArr = [NSMutableArray array];
        for (NSDictionary *dict in array) {
        InterfaceMessage *model = [InterfaceMessage interfaceMessageWithDict:dict];
                [interArr addObject:model];
        }
                _friendsMess =interArr;
    }
            return _friendsMess;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUpAllControl];
    [self messages];
}
-(void)setTableView{
    _friendstab = [[UITableView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    _friendstab.rowHeight = 60;
    //边缘布局下移
    
   self.edgesForExtendedLayout = UIRectEdgeNone;
     self.automaticallyAdjustsScrollViewInsets =    NO;
    [self.view addSubview:_friendstab];
    _friendstab.delegate = self;
    _friendstab.dataSource = self;
    self.navigationItem.title = @"消息";
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"超级截屏_20161117_180321"] forBarMetrics:UIBarMetricsDefault];
}
-(void)setUpAllControl{
     self.view.backgroundColor = [UIColor redColor];
    [self setNavigationRightButtonAndGrouping];
    [self setAddPicture];
    [self setTableView];
}
-(void)setNavigationRightButtonAndGrouping{
    //添加分组控件（消息、电话）
    UISegmentedControl *segmen = [[UISegmentedControl alloc]initWithItems:@[@"消息",@"电话"]];
    [segmen addTarget:self action:@selector(segmenAction:) forControlEvents:UIControlEventValueChanged];
    self.navigationItem.titleView =segmen;
    //添加消息导航右按钮
    UIBarButtonItem *barButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(addAction)];
    self.navigationItem.rightBarButtonItem = barButton;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MessageTableViewCell *cell = [MessageTableViewCell cellWithTableView:tableView];
    InterfaceMessage *cellDat =self.friendsMess[indexPath.row];
    cell.cellData =cellDat;
    [cell setUPControlProperties];
   // cell.accessoryType =
    //UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.friendsMess.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ChatViewController *chat =[[ChatViewController alloc]init];
  //  LWBNavgaViewController *nav = [[LWBNavgaViewController alloc]initWithRootViewController:chat];
    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:chat animated:YES];
    chat.title =[_friendsMess[indexPath.row]valueForKey:@"name"];
    self.hidesBottomBarWhenPushed = NO;
}
-(void)setAddPicture{
    //添加add图片
    self.addImag = [[UIImageView alloc]initWithFrame:CGRectMake(190,67, 120,200)];
    self.addImag.image = [UIImage imageNamed:@"qq添加图片"];
    self.addImag.hidden = YES;
    [self.view addSubview:self.addImag];
    
}
-(void)addAction{
    if (self.addImag.hidden==YES) {
        self.addImag.hidden = NO;
    }else{
        self.addImag.hidden = YES;
    }
}

-(void)segmenAction:(id)sender{
     PhoneViewController *phoneVi = [[PhoneViewController alloc]init];
    MessageViewController *messa =[[MessageViewController alloc]init];
    switch ([sender selectedSegmentIndex]) {
        case 1:
            [self.view addSubview:phoneVi.view];
            [self.view addSubview:self.addImag];
            break;
        default:
            [self.view addSubview:messa.view];
            [self.view addSubview:self.addImag];
            break;
    }
}
-(void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    NSSet *allTouches = [event allTouches];
    UITouch *touch = [allTouches anyObject];
    CGPoint point = [touch locationInView:[touch view]];
    int x = point.x;
    int y = point.y;
     NSLog(@"touch (x, y) is (%f, %f)", point.x,point.y);
    if (!(x>190&&y>67&&y<267&&y>67)) {
//        touches.
      //  NSLog(@"%@,%@",[touches anyObject].view,self.addImag);
        _addImag.hidden = YES;
   }
}
//- (CGPoint)locationInView:(nullable UIView *)view{
//    
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
//左滑
//-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
//    if (editingStyle == UITableViewCellEditingStyleInsert) {
//        //1、删除数据模型
//        [self.friendsMess removeObjectAtIndex:indexPath.row];
//        //2、刷新表视图
//        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
//        
//    }
//}
//-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    
//    return YES;
//}
//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath

//{
    
 //   return UITableViewCellEditingStyleDelete;
    
//}
//
- (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewRowAction *overhead =[UITableViewRowAction rowActionWithStyle: UITableViewRowActionStyleDefault  title:@"顶置" handler:^(UITableViewRowAction *action,NSIndexPath *indexPath){
        NSIndexPath *firstIndexPath =
        [NSIndexPath indexPathForRow:0 inSection:indexPath.section];
        [tableView moveRowAtIndexPath:indexPath toIndexPath:firstIndexPath];
        
    }];
    
    UITableViewRowAction *Delete =[UITableViewRowAction rowActionWithStyle: UITableViewRowActionStyleDefault  title:@"删除" handler:^(UITableViewRowAction *action,NSIndexPath *indexPath){
                 //1、删除数据模型
        [self.friendsMess removeObjectAtIndex:indexPath.row];
        //删除该行
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];}];
    Delete.backgroundColor = [UIColor brownColor];
    return @[overhead,Delete];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
